﻿using OestsCommon.Model;
using System;
using System.Collections.Generic;
using System.Data;

namespace OestsDataBase
{
    public static class ProjectDAL
    {

        //项目表 对应实训 考试
        public static void CreateTable()
        {
            string sqlCheck = "if object_id( 'T_Project') is not null select 1 else select 0";
            if ((int)SqlHelper.ExecuteScalar(sqlCheck) == 0)
            {
                string sqlCreacte = @"CREATE TABLE T_Project (id bigint primary key ,
name NVARCHAR(MAX) not null,major_id bigint not null，subjects_id bigint,overlap bit not null)";
                SqlHelper.ExecuteNonQuery(sqlCreacte);
            }
        }
        public static List<ProjectModel> GetData()
        {
            List<ProjectModel> list = new List<ProjectModel>();
            string strSql = "select * from T_Project";
            DataTable dt = SqlHelper.ExecuteDataTable(strSql);
            if (dt == null) { return null; }
            foreach (DataRow dr in dt.Rows)
            {
                ProjectModel s = ReadDataRow(dr);
                list.Add(s);
            }
            return list;
        }

        private static ProjectModel ReadDataRow(DataRow dr)
        {
            ProjectModel p = new ProjectModel
            {
                Id = Int32.Parse(dr["id"].ToString()),
                Name = dr["name"].ToString(),
                MajoiId = Int32.Parse(dr["major_id"].ToString()),
                SubjectsId = dr["subjects_id"].Equals(DBNull.Value) ? Int32.Parse(dr["id"].ToString()) : Int32.Parse(dr["subjects_id"].ToString()),
                IsOverlap = (bool)dr["overlap"]

                //  Environment = (TrainingEnvironment)(dr["environment"].Equals(DBNull.Value) ? 0 : Int32.Parse(dr["environment"].ToString())),
                //  ExamPaperId = dr["examPaperId"].Equals(DBNull.Value) ? 0 : Int32.Parse(dr["examPaperId"].ToString()),
            };
            return p;
        }

        /// <summary>
        /// 根据项目Id获取该项目对应的题库id
        /// </summary>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public static int GetSubjectsIDByProjectID(int projectId)
        {
            string sqlCheck = "select subjects_id from T_Project where id=" + projectId.ToString();
            object obj = SqlHelper.ExecuteScalar(sqlCheck);
            int subjectsId = obj.Equals(DBNull.Value) ? projectId : int.Parse(obj.ToString());
            return subjectsId;
        }
        //public static bool SetEnvironment(int projectId, TrainingEnvironment en, int examPaperId)
        //{
        //    string sql = string.Format("update T_Project set environment={1},examPaperId={2}  where id={0}", projectId, (int)en, examPaperId);
        //    int n = SqlHelper.ExecuteNonQuery(sql);
        //    return n == 1;
        //}


        //public static DP<TrainingEnvironment, int> GetEnvironmentByProject(int projectId)
        //{
        //    string sql = string.Format("select environment,examPaperId from T_Project where id={0}", projectId);
        //    DataTable dt = SqlHelper.ExecuteDataTable(sql);
        //    if (dt == null) return null;
        //    if (dt.Rows.Count == 1)
        //    {
        //        DataRow dr = dt.Rows[0];
        //        DP<TrainingEnvironment, int> data = new DP<TrainingEnvironment, int>();

        //        data.Value1 = (TrainingEnvironment)(dr["environment"].Equals(DBNull.Value) ? 0 : Int32.Parse(dr["environment"].ToString()));
        //        data.Value2 = dr["examPaperId"].Equals(DBNull.Value) ? 0 : Int32.Parse(dr["examPaperId"].ToString());
        //        return data;
        //    }

        //    return null;

        //}

    }
}
